package com.siruier.boss.ui.activity.mall

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.siruier.boss.api.MALL_API_SERVICE
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.api.core.toData
import com.siruier.boss.bean.MallGoodsBean
import com.siruier.boss.common.GOODS_SPAN_COUNT
import com.siruier.boss.databinding.ActivityStoreSearchBinding
import com.siruier.boss.ui.adapter.GoodsAdapter
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.ext.showEmptyCallback
import com.siruier.boss.ui.helper.RefreshHelper

class GoodsSearchActivity : BaseActivity<ActivityStoreSearchBinding>() {
    private lateinit var mRefreshHelper: RefreshHelper<MallGoodsBean>
    private val mGoodsAdapter: GoodsAdapter = GoodsAdapter()
    private var mLayoutManager: GridLayoutManager? = null
    override fun initView() {
        bindViewClick(vb.ivToUp)
        mRefreshHelper = RefreshHelper(vb.refreshLayout, vb.rvGoods, mGoodsAdapter, listener = ::goodsList)
        mLayoutManager = GridLayoutManager(this, GOODS_SPAN_COUNT)
        vb.rvGoods.layoutManager = mLayoutManager
        vb.searchView.hint = "搜索您想要的商品"
        vb.searchView.clearListener = ::goodsList
        mRefreshHelper.mLoadService?.showEmptyCallback()
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.ivToUp -> {
                mLayoutManager?.scrollToPositionWithOffset(0, 0)
            }
        }
    }

    private fun goodsList() {
        launchUI({ MALL_API_SERVICE.goodsList(vb.searchView.text, null, mRefreshHelper.page).toData()?.records }, mRefreshHelper.handApiObserver())
    }
}